package com.example.productservice.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @author lenovo
 * @Description
 * @Date2025/4/17 下午3:15
 */
@RestController

public class HelloController {

    @SentinelResource(value = "hello",blockHandler = "handleHello",blockHandlerClass = ExceptionHandler.class )
    @GetMapping("/hello")
    public String hello(){
        System.out.println(Thread.currentThread().getName()+"-->"+System.currentTimeMillis());
        return "hello sentinel!!";
    }


//    自定义流控处理
//    public String handleHello(BlockException ex){
//        return "这是hello接口被限流了";
//
//    }

    @RequestMapping("/order")
    @SentinelResource(value = "order",fallback = "orderFallback")
    public String order(){
        Random random = new Random();
        if(random.nextInt(2)==0){
            throw new RuntimeException("错误");
        }

        return "这是订单";
    }

    public String orderFallback(){
        return "这是降级订单";
    }


    @RequestMapping("/payment")
    public String payment(){
        return "这是支付";
    }

}